package com.ysalliance.getFan.organization.ui;

import android.content.Intent;
import android.text.InputFilter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.google.gson.Gson;
import com.ysalliance.getFan.organization.Bean.ClassBean;
import com.ysalliance.getFan.organization.Bean.OrganizationBean;
import com.ysalliance.getFan.organization.Bean.bus.ChangeClassNameBean;
import com.ysalliance.getFan.organization.Bean.bus.DeleteClassBean;
import com.ysalliance.getFan.organization.LucasUtil;
import com.ysalliance.getFan.organization.R;
import com.ysalliance.getFan.organization.network.Network;
import com.ysalliance.getFan.organization.ui.base.LucasBaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import androidx.annotation.NonNull;
import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

import static com.ysalliance.getFan.organization.FanerConstant.CHANGE_CLASS_NAME;
import static com.ysalliance.getFan.organization.FanerConstant.SET_CLASS_NAME;
import static com.ysalliance.getFan.organization.FanerConstant.SET_TASK_NAME;


/**
 * EditText一项输入
 * @author lucas
 * created at 2020-02-24 14:54
 */
public class EditTextOneActivity extends LucasBaseActivity {

    //用户协议
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_create_class)
    TextView tv_create_class;
    @BindView(R.id.et_content)
    EditText et_content;


    public ClassBean classBean ;

    public int edit_class;  //这个页面的类型
    private OrganizationBean organization;  //机构的类型


    @Override
    public int bindContentView() {
        return R.layout.activity_enter_create_class;
    }

    @Override
    public void afterView() {
        super.afterView();

        edit_class = getIntent().getIntExtra("class", 0);
        organization = (OrganizationBean) getIntent().getSerializableExtra("organization");


        tv_title.setText("设置班级名称");

        if(edit_class == SET_TASK_NAME){
            //添加任务
            tv_title.setText("添加任务");
            et_content.setHint("100个字符以内");
            et_content.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100)});
        }else if(edit_class == SET_CLASS_NAME){
            //设置班级名称
            tv_title.setText("设置班级名称");
            et_content.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
        }else if(edit_class == CHANGE_CLASS_NAME){
            //修改班级名称
            classBean = (ClassBean) getIntent().getSerializableExtra("class_info");
            tv_title.setText("修改班级名称");
            et_content.setText(classBean.getName());
            et_content.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
        }




        initClick();

    }


    public void initClick() {
        //完成按钮的点击事件
        tv_create_class.setOnClickListener(v->{
            String class_name = et_content.getText().toString().trim();
            if(edit_class == SET_TASK_NAME){
                //添加任务
                Intent intent = new Intent();
                intent.putExtra("task_name", class_name);
                setResult(RESULT_OK, intent);
                finish();

            }else if(edit_class == SET_CLASS_NAME){
                //设置班级名称
                if (class_name.length()<1){
                    return;
                }
                creat_class(class_name, organization.getId());
            }else if(edit_class == CHANGE_CLASS_NAME){
                //修改班级名称
                if (class_name.length()<1){
                    return;
                }
                change_class_name(classBean.getId(), class_name);
            }



        });



    }




    /**  创建班级   */
    public void creat_class(String name, String institution){

        disposable = Network.getUtilApi()
                .creat_class(name, institution)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(@NonNull ResponseBody responseBody) throws Exception {
                        String res = responseBody.string();
                        LogUtils.json("获取组织结构的返回", res);

                        JSONObject jsonObject = new JSONObject(  res  );
                        if(LucasUtil.isok(jsonObject.optString("status"))){

                            JSONObject jsonObject_data = new JSONObject(  jsonObject.optString("data") );

                            ClassBean classBean = new Gson().fromJson( jsonObject_data.optString("class"), ClassBean.class);

                            Intent intent = new Intent(Utils.getApp(), EnterCreateClassSuccessActivity.class);
                            intent.putExtra("class_info",classBean);
                            ActivityUtils.startActivity(intent);
                            finish();

                        }else {
                            ToastUtils.showShort(jsonObject.optString("message"));
                        }
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                        LogUtils.e(throwable.getMessage());
                        Toast.makeText(mContext,"网络请求失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    /**  修改班级名称   */
    public void change_class_name(String class_id, String name){

        disposable = Network.getUtilApi()
                .change_class_name(class_id, name)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(@NonNull ResponseBody responseBody) throws Exception {
                        String res = responseBody.string();
                        LogUtils.json("修改班级名称的返回", res);

                        JSONObject jsonObject = new JSONObject(  res  );
                        if(LucasUtil.isok(jsonObject.optString("status"))){

                            EventBus.getDefault().post(new ChangeClassNameBean(name));
                            finish();

                        }else {
                            ToastUtils.showShort(jsonObject.optString("message"));
                        }
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                        LogUtils.e(throwable.getMessage());
                        Toast.makeText(mContext,"网络请求失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }





}
